package com.nxmu.hire.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nxmu.hire.common.exception.ServiceException;
import com.nxmu.hire.dao.PostMapper;
import com.nxmu.hire.model.EnumMap.EnumMap;
import com.nxmu.hire.model.Post;
import com.nxmu.hire.model.PostExample;
import com.nxmu.hire.service.PostService;
import com.nxmu.hire.utils.HireUtil;
import com.nxmu.hire.vo.AddPostReqVo;
import com.nxmu.hire.vo.FindPostListByPageReqVo;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class PostServiceImpl implements PostService {

    private final static Logger LOGGER = LoggerFactory.getLogger(PostServiceImpl.class);

    @Autowired
    private PostMapper postMapper;

    /**
     * 添加教学岗位
     * @param addPostReqVo
     */
    @Transactional
    @Override
    public void addPost(AddPostReqVo addPostReqVo) throws ServiceException {
        Post post = new Post();
        BeanUtils.copyProperties(addPostReqVo,post);
        post.setCreateTime(new Date());
        post.setLastUpdateTime(new Date());
        post.setSchoolYear(HireUtil.countSchoolYear(LocalDate.now()));
        post.setSemester(HireUtil.countSemester(LocalDate.now()));
        post.setCollegeName(EnumMap.collegeEnumMap.get(addPostReqVo.getCollegeId()));
        try {
            postMapper.insert(post);
        } catch (Exception e) {
            LOGGER.error("岗位信息保存失败！");
            throw new ServiceException("岗位信息已存在，保存失败！");
        }
    }

    /**
     * 查询教学岗位
     * @param findPostListByPageReqVo
     * @return
     */
    @Override
    public PageInfo<Post> findPostListByPage(FindPostListByPageReqVo findPostListByPageReqVo) {
        PageHelper.startPage(findPostListByPageReqVo.getPageNum(),findPostListByPageReqVo.getPageSize());
        PostExample postExample = new PostExample();
        PostExample.Criteria criteria = postExample.createCriteria()
                .andSchoolYearGreaterThanOrEqualTo(HireUtil.countSchoolYear(LocalDate.now()))
                .andSemesterEqualTo(HireUtil.countSemester(LocalDate.now()));
        if(!Objects.isNull(findPostListByPageReqVo.getCollegeId())) criteria.andCollegeIdEqualTo(findPostListByPageReqVo.getCollegeId());
        List<Post> posts = postMapper.selectByExample(postExample);
        return new PageInfo<>(posts);
    }

    /**
     * 修改教学岗位数量
     * @param postId
     * @param postCount
     * @throws ServiceException
     */
    @Transactional
    @Override
    public void updatePostCountByPostId(Integer postId, Integer postCount) throws ServiceException {
        Post post = postMapper.selectByPrimaryKey(postId);
        post.setPostId(postId);
        post.setPostCount(postCount);
        try {
            postMapper.updateByPrimaryKey(post);
        } catch (Exception e) {
            LOGGER.error("修改教学岗位数量失败！");
            throw new ServiceException("修改教学岗位数量失败！");
        }
    }
}
